package sample;

import java.awt.Color;
import java.io.IOException;
import java.util.Date;

import robocode.HitRobotEvent;
import robocode.ScannedRobotEvent;
import robocode.TeamRobot;

public class RoboRefem extends TeamRobot {

	private boolean safe = false;
	private Date tempoPassou = new Date();


	
	@Override
	public void run() {

		setRadarColor(Color.RED);
		while (true) {
			
			turnRadarRight(360);
			execute();
			
			System.out.println("TRES SEGUNDOS:  "+(System.currentTimeMillis() - tempoPassou.getTime()));
			if((System.currentTimeMillis() - tempoPassou.getTime())>=5000){
				System.out.println("TRES SEGUNDOS");
				MensagemTime m = new MensagemTime(getName(), getX(), getY(),false);
				try {
					sendMessage("sample.RoboScanner", m);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				tempoPassou.setTime(System.currentTimeMillis());
			}
			
		}
	}

	@Override
	public void onScannedRobot(ScannedRobotEvent event) {
		/*
		 * if (!event.getName().startsWith("sample.RoboScanner") &&
		 * !event.getName().startsWith("sample.RoboInimigo") &&
		 * event.getDistance() <= 700) {
		 */
		if (event.getName().startsWith("sample.RoboJogador")
				&& event.getDistance() <= 700) {
			clearAllEvents();
			if (!safe && event.getDistance() > 25) {
				setTurnRight(event.getBearing());
				setAhead(20);
			} else {
				doNothing();
			}
			if (this.getX() < 200)
				safe = true;
			execute();
		}
	}

	@Override
	public void onHitRobot(HitRobotEvent event) {
		// Random r = new Random();
		// double n = r.nextDouble();
		// if( n < 0.4 ){
		// setTurnLeft(90);
		// setBack(80);
		// }else if( n > 0.4 && n < 0.7 ){
		// setTurnRight(90);
		// setAhead(80);
		// }else{
		// setTurnLeft(180);
		// setAhead(80);
		// }
		setTurnRight(90);
		ahead(50);
		setTurnLeft(90);
		execute();
		// setStop();
		// execute();
	}

}